package com.tanhua.server.interceptor;

import com.tanhua.model.db.User;

/**
 * 操作Threadlocal的工具类
 */
public class UserHolder {

    /**
     * 定义ThreadLocal通过泛型，执行存入/取出的数据类型
     */
    private static  ThreadLocal<User> tl = new ThreadLocal<>();

    //将用户存入当前线程（tl）
    public static void setUser(User user) {
        tl.set(user);
    }

    //从tl取出用户
    public static User getUser() {
        return tl.get();
    }

    //将tl数据清空
    public static void remove() {
        tl.remove();;
    }

    //获取用户的id
    public static Long getUserId() {
        User user = tl.get();
        return user.getId();
    }
}
